package com.mall.order.biz.handler;

import com.mall.commons.tool.exception.BizException;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.DeleteCheckedItemRequest;
import com.mall.shopping.dto.DeleteCheckedItemResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

/**
 * 将购物车中的缓存失效
 * @author Javid Xi
 * @version 1.0
 * @since 2021/7/20
 */
@Slf4j
@Component
public class ClearCartItemHandler extends AbstractTransHandler {

    @Reference(timeout = 3000,check = false)
    ICartService cartService;

    //是否采用异步方式执行
    @Override
    public boolean isAsync() {
        return false;
    }

    /**
     * 从购物车中删除下单商品
     * @param context 上下文
     * @return boolean
     */
    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext orderContext= (CreateOrderContext) context;
        Long userId = orderContext.getUserId();

        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(userId);
        // 删除
        DeleteCheckedItemResponse response = cartService.deleteCheckedItem(request);
        if(!response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            throw new BizException(response.getCode(), response.getMsg());
        }
        return true;
    }
}
